<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
  import echarts from 'echarts';
  import resize from './mixins/resize';

  require('echarts/theme/macarons');// echarts theme
  require('echarts/theme/gray');
  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '550px'
      },
      autoResize: {
        type: Boolean,
        default: true
      },
      impChartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        chart: null
      }
    },
    watch: {
      impChartData: {
        deep: true,
        handler(val) {
          this.setOptions(val);
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart();
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'gray');
        this.setOptions(this.impChartData);
      },
      setOptions({xData, spontaneousData, salesmanData, residentData, employeeData, lookData, activityData, personData} = {}) {
        this.chart.setOption({
          xAxis: {
            type: 'category',
            data: xData,
            boundaryGap: false,
            axisTick: {
              show: false
            },
            axisLabel: {//文字样式
              textStyle: {
                color: '#787A80',
                fontSize: '12'
              },
            }
          },
          grid: {
            bottom: 150,
            top: 50,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10]
          },
          yAxis: {
            axisTick: {
              show: false
            },
            name: '（次）',
            nameTextStyle: {
              color: '#D2D4D9'
            },
            type: 'value',
            axisLabel: {//文字样式
              textStyle: {
                color: '#787A80',
                fontSize: '12'
              },
            },
            splitLine: {// 控制网格线
              lineStyle: {
                color: '#eee'
              }
            }
          },
          legend: {
            data: ['蓝聘余额', '圈子', '招工', '积分', '看看', '活动中心', '个人中心'],
            bottom: 'bottom',
            borderWidth: 1,
            borderColor: '#D8DCE5',
            backgroundColor: '#F7F8FA',
            padding: [20, 120, 40, 120],
            itemGap: 30
          },
          series: [{
            name: '蓝聘余额',
            itemStyle: {
              normal: {
                color: '#3CAFFF',
                lineStyle: {
                  color: '#3CAFFF',
                  width: 2
                }
              }
            },
            type: 'line',
            data: spontaneousData,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
            {
              name: '圈子',
              type: 'line',
              itemStyle: {
                normal: {
                  color: '#1ED7DE',
                  lineStyle: {
                    color: '#1ED7DE',
                    width: 2
                  }
                }
              },
              data: salesmanData,
              animationDuration: 2800,
              animationEasing: 'quadraticOut'
            },
            {
              name: '招工',
              type: 'line',
              itemStyle: {
                normal: {
                  color: '#08CC84',
                  lineStyle: {
                    color: '#08CC84',
                    width: 2
                  }
                }
              },
              data: residentData,
              animationDuration: 2800,
              animationEasing: 'quadraticOut'
            },
            {
              name: '积分',
              type: 'line',
              itemStyle: {
                normal: {
                  color: '#FFCB3D',
                  lineStyle: {
                    color: '#FFCB3D',
                    width: 2
                  }
                }
              },
              data: employeeData,
              animationDuration: 2800,
              animationEasing: 'quadraticOut'
            },
            {
              name: '看看',
              type: 'line',
              itemStyle: {
                normal: {
                  color: '#AB8AE6',
                  lineStyle: {
                    color: '#AB8AE6',
                    width: 2
                  }
                }
              },
              data: lookData,
              animationDuration: 2800,
              animationEasing: 'quadraticOut'
            },
            {
              name: '活动中心',
              type: 'line',
              itemStyle: {
                normal: {
                  color: '#FF913D',
                  lineStyle: {
                    color: '#FF913D',
                    width: 2
                  }
                }
              },
              data: activityData,
              animationDuration: 2800,
              animationEasing: 'quadraticOut'
            },
            {
              name: '个人中心',
              type: 'line',
              itemStyle: {
                normal: {
                  color: '#FF5656',
                  lineStyle: {
                    color: '#FF5656',
                    width: 2
                  }
                }
              },
              data: personData,
              animationDuration: 2800,
              animationEasing: 'quadraticOut'
            }
          ]
        })
      }
    }
  }
</script>
